import os
import shutil
import sys

import cv2
import imageio
import imgaug.augmenters as iaa

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root)

data = os.path.join(root, "datasets")


def aug(image_path, outdir):
    if os.path.exists(outdir):
        shutil.rmtree(outdir)
        os.makedirs(outdir)
    else:
        os.makedirs(outdir)

    image = imageio.imread(image_path)
    imageio.imwrite(os.path.join(outdir, "0_sigma_0.00.jpeg"),image)

    for i, sigma in enumerate(range(1, 6, 1)):
        image = imageio.imread(image_path)
        seq = iaa.Sequential([
            iaa.imgcorruptlike.GaussianBlur(severity=sigma)
        ])
        image_aug = seq(image=image)
        imageio.imwrite(os.path.join(outdir, "{}_sigma_{:.2f}.jpeg".format(i + 1, sigma)), image_aug)


def resize(input_path, output_path, size=((1227//4), (784//4))):
    for file in os.listdir(input_path):
        image = cv2.imread(os.path.join(input_path, file))
        if size:
            image = cv2.resize(image, size)
        cv2.imwrite(os.path.join(output_path, file), image)


aug("card.jpg", os.path.join(root, "datasets", "grd_test", "fuzzy words"))
resize(os.path.join(root, "datasets", "grd_test", "fuzzy words"),
       os.path.join(root, "datasets", "grd_test", "fuzzy words"))
